const sign = require('../common/lib/sign');
module.exports = (options, app) => {
  return async(ctx, next) => {
    let data = null;
    if (ctx.request.req.method === 'GET') { //  get  请求
      data = ctx.request.query;
    } else {
      data = ctx.request.body.post;
    }
    if (!think.isEmpty(data)) { // 参数为空不校验sign
      if (!data.sign) {
        ctx.body = {
          errno: 1001,
          url: ctx.req._parsedUrl.pathname,
          errmsg: '签名不能为空！'
        };
        return ctx.body;
      } else if (sign.getSign(data) !== data.sign) {
        ctx.body = {
          errno: 1001,
          url: ctx.req._parsedUrl.pathname,
          sign: sign.getSign(data),
          errmsg: '无效的签名!'
        };
        return ctx.body;
      }
    }
    await next();
  };
};
